﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Box.BL;
using Box.Entities;
using Microsoft.Practices.Unity;

namespace Box.Cliente.Logistica.SerieDocumento
{
    public partial class frmSeries : System.Web.UI.Page
    {
        [Dependency]
        public ISerieComprobanteBL serieBL { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var acc = Request.QueryString["acc"];
                var id = Request.QueryString["id"];

                if (acc==null)
                {
                    
                    Response.Redirect("frmListSeries.aspx");
                }
                if (acc!=null && id!=null && acc=="editar")
                {
                    var identificador = int.Parse(id);

                    var serie = serieBL.GetSerieComprobanteId(identificador);
                    txtCodigo.Text = serie.Id.ToString();
                    txtSerie.Text = serie.SerieBoleta.ToString();
                    txtIniCorre.Text = serie.IniCorrelativoFactura.ToString();
                }
            }
        }

        protected void btnGuardar_Click(object sender, EventArgs e)
        {
            var accion = Request.QueryString["acc"];

            var serie = new SerieComprobante()
            {
                SerieBoleta = txtSerie.Text,
                IniCorrelativoBoleta = txtIniCorre.Text
            };

            if (accion =="new")
            {
                serieBL.AddSerieComrpabante(serie);
                Response.Redirect("frmListSeries.aspx");
                
            }else if (accion=="editar")
            {
                serie.Id = int.Parse(txtCodigo.Text);
                if (int.Parse(txtSerie.Text)>0)
                {
                    serieBL.UpdateSerieComprabante(serie);
                    Response.Redirect("frmListSeries.aspx");
                }
            }
        }
    }
}